#ifndef __DATE_HPP__
#define __DATE_HPP__

#include <cstdio>
#include <string>
#include <sstream>
#include <iomanip>

using std::string;
using std::stringstream;
using std::setfill;
using std::setw;
// using std::right;

class Date
{
public:
    bool valid;
    bool unknown;
    int year;
    int month;
    int day;

    Date();
    Date(const string &s);
    Date(int y, int m, int d);
    Date(const Date &date);
    Date& operator=(const Date &date);

    void setValid(bool vd = true);
    void setUnknown(bool uk = true);
    void setDate(int y, int m, int d);

    string toString();
};

Date::Date() : valid(false) 
{}

Date::Date(int y, int m, int d)
{
    setDate(y, m, d);
}

Date::Date(const string &s)
{
    if (s == "-")
    {
        valid = false;
        return;
    }
    if (s == "?")
    {
        valid = true;
        unknown = true;
    }
    // TODO 处理非法日期
    int ret = sscanf(s.c_str(), "%d-%d-%d", &year, &month, &day);
    valid = true;
    unknown = false;
}

Date::Date(const Date &date)
{
    valid = date.valid;
    unknown = date.unknown;
    year = date.year;
    month = date.month;
    day = date.day;
}

void Date::setUnknown(bool uk)
{
    valid = true;
    unknown = uk;
}

void Date::setValid(bool vd)
{
    valid = vd;
}

void Date::setDate(int y, int m, int d)
{
    valid = true;
    unknown = false;
    year = y;
    month = m;
    day = d;
}

Date& Date::operator=(const Date &date)
{
    valid = date.valid;
    unknown = date.unknown;
    year = date.year;
    month = date.month;
    day = date.day;
    return *this;
}

string Date::toString()
{
    if (!valid) return "";

    if (unknown) return "?";

    string dateStr;
    stringstream ss;
    ss << setw(4) << setfill(' ') << year << "-" 
        << setw(2) << setfill('0') << month << "-" 
        << setw(2) << setfill('0') << day;
    ss >> dateStr;
    return dateStr;
}

#endif
